//
//  L_UserInfoVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/13.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import MBProgressHUD

class L_UserInfoVC: BaseViewControllerWithHeader {
    let nameLab = UILabel()
    let groupLab = UILabel()
    let phoneLab = UILabel()
    
    var smsVerifyCredential : String
    var vaildeCode : String
    var mobile : String
    //
    var accountAtiveTicket = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
//        getActivatedUser()
    }
    
    init(smsVerify:String,code:String,mob:String) {
        smsVerifyCredential = smsVerify
        vaildeCode = code
        mobile = mob
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func getActivatedUser(callback: @escaping (Bool,APIResult) -> Void) {
        let params :[String:Any] = ["mobile":mobile,
                                    "smsVerifyCredential":smsVerifyCredential,
                                    "vaildeCode":vaildeCode]
        MBProgressHUD.showOrIncrease()
        API.request(url: .L_ActivatedUser, params: params) { (result) in
            if result.success {
                MBProgressHUD.hideOrDecrease()
                if result.data?.getBool("success") ?? false {
                    if let dl = result.data?.getDictionary("data") {
                        self.nameLab.text = dl.getString("name")
                        self.groupLab.text = dl.getString("departmentName")
                        self.phoneLab.text = self.mobile
                        self.accountAtiveTicket = dl.getString("accountActiveTicket") ?? ""
                    }
                }
                callback(true,result)
            }else{
                MBProgressHUD.hideOrDecrease()
                let popup = PopupSureBox(title: result.errMsg ?? "无法获取激活信息", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
                }
                callback(false,result)
            }
        }
    }
}

// MARK: UI
extension L_UserInfoVC {
    func setupUI() {
        safeView.backgroundColor = .xF2F2F2
        headView.backgroundColor = .white
        headTitle.text = "个人信息"

        
        var bgview = safeView.drawView { (make) in
            make.top.equalTo(headView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(51.5)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "姓名", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: nameLab,text: nameLab.text, fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        //画横线
        var line = bgview.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        bgview = safeView.drawView { (make) in
            make.top.equalTo(bgview.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(51.5)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "所属组织", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: groupLab,text: groupLab.text, fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        //画横线
        line = bgview.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        bgview = safeView.drawView { (make) in
            make.top.equalTo(bgview.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(75)
        }
        bgview.backgroundColor = .white
        bgview.drawLabel(text: "手机号码", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview().offset(-10)
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: phoneLab,text: phoneLab.text, fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.centerY.equalToSuperview().offset(-10)
            make.right.equalToSuperview().offset(-10)
        }
        bgview.drawLabel(text: "*如需修改手机号，请您提交书面申请到人事备案", fontSize: 12, fontWeight: .regular, textColor: .x999999) { (make) in
            make.centerY.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }.numberOfLines = 2
        
        let btn = safeView.drawButton(title: "下一步", titleColor: .white, fontSize: 15) { (make) in
            make.top.equalTo(bgview.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(35)
        }
        btn.backgroundColor = .xFC4C02
        btn.layer.cornerRadius = 7
        btn.addTarget(self, action: #selector(_submiButtonClick(button:)), for: .touchUpInside)
    }
}

// MARK: 事件
extension L_UserInfoVC {
    @objc func _submiButtonClick(button: Button) {
        let vc = L_ActivePwdVC(phoneNum: mobile, accountAtiveTicket: accountAtiveTicket)
        pushViewController(vc)
    }
}
